--
-- Author: shunguo.chen
-- Date: 2018-01-29 11:02:15
--
-- MiniMap 主界面右上角地图展示

local ClassRef = app.mvc.createPanelClass(...)

function ClassRef.create(node)
    local sprite = ClassRef.extend(node)
    sprite:init()
    return sprite
end

local signBar = nil
local energyBar = nil

local function refreshMachine()
	if Config.deviceAPI then
		local ele=device.getBatteryLevel()
		energyBar:setPercent(ele)
	else
		energyBar:setPercent(100)
	end
	local s=app.session:getSignalLevel()*35
	signBar:setPercent(s)
end

function ClassRef:init(data)
    self.super.init(self, data)

    signBar = self._ui["Panel/signal/bar"]
    energyBar = self._ui["Panel/ele/bar"]

    self:schedule(refreshMachine, 5)

    display.uiAddClick(self._ui.img, function()
    	executeFunc(self._onClick)
    end)
    display.uiAddClick(self._ui.lion, function()
    	executeFunc(self._onClick)
    end)
end

function ClassRef:remove()
	signBar = nil
	energyBar = nil
end

function ClassRef:getFuncBtn(name)
	return self._ui[name]
end

function ClassRef:hideBtns()
	if self._oldState then
		return
	end
	self._oldState = {}
	local keys = {"Panel/button_71", "Panel/button_72", "Panel/button_73", "Panel/button_74"}

	for k, v in pairs(keys) do
		self._oldState[v] = self._ui[v]:isVisible()
		self._ui[v]:setVisible(false)
	end
	self._ui["Panel/Panel_bg/bg2"]:setVisible(false)
end

function ClassRef:resetBtns()
	if not self._oldState then
		return
	end
	for k, v in pairs(self._oldState) do
		self._ui[k]:setVisible(v)
	end
	self._oldState = nil
	self._ui["Panel/Panel_bg/bg2"]:setVisible(true)
end


-- data = mapId
function ClassRef:refresh()
	if self._data then
		local conf = GD:queryMapById(self._data)
		self._ui.map_name:setString(L(conf.string))

		if conf.icon then
			self._ui.map_img:loadTexture( table.concat( { "art/main_mapimg/",  conf.icon, ".png" } ) )
		end

		local mapType = tonumber( conf.maptype )
		if app:getInst( "NewActivityDungeonsModel" ):isChampionMap() then -- 冠军赛地图做特殊处理
			self._ui.map_img:setVisible( false )
			self._ui.lion:setVisible( true )
		elseif UD.csGuildWarData:isCSGuildWarPrepareMap() then
			self._ui.map_img:setVisible( false )
			self._ui.lion:setVisible( true )
		elseif mapType == 3 then -- 公会战地图
			self._ui.map_img:setVisible( false )
			self._ui.lion:setVisible( true )
		elseif 2 == mapType or 7 == mapType or 9 == mapType then
			self._ui.map_img:setVisible( false )
			self._ui.lion:setVisible( true )
		else
			self._ui.map_img:setVisible( true )
			self._ui.lion:setVisible( false )
		end
	end
end

function ClassRef:setControlCallback(cb)
	self._onClick = cb
end

return ClassRef
